package com.example.administrator.myapplication.activity.anchorSecondActivity;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.databinding.DataBindingUtil;
import android.support.v4.util.Pair;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.BoringLayout;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.administrator.myapplication.BR;
import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.activity.anchorSecondActivity.bean.AnchorSecondAvtivityAblumData;
import com.example.administrator.myapplication.activity.anchorSecondActivity.bean.AnchorSecondAvtivityAblumEntity;
import com.example.administrator.myapplication.activity.anchorSecondActivity.bean.AnchorSecondAvtivityEntity;
import com.example.administrator.myapplication.activity.anchorSecondActivity.bean.AnchorSecondAvtivityVoiceData;
import com.example.administrator.myapplication.activity.anchorSecondActivity.bean.AnchorSecondAvtivityVoiceEntity;
import com.example.administrator.myapplication.callback.XmlyService;
import com.example.administrator.myapplication.databinding.ActivityAnchorSecondBinding;
import com.example.administrator.myapplication.until.BaseApplication;
import com.example.administrator.myapplication.until.CommentRecyclerAdapter;
import com.example.administrator.myapplication.until.DatabaseHelper;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AnchorSecondActivity extends AppCompatActivity implements View.OnClickListener {
    private ActivityAnchorSecondBinding binding;
    private String uid;
    private CommentRecyclerAdapter<Object> adapter;
    private XmlyService service;
    private boolean isClose;
    private BaseApplication application;
    private String mobileLargeLogo;
    private String name;
    private boolean isClick = false ;
    private DatabaseHelper helper;
    private  SQLiteDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_anchor_second);
        uid = getIntent().getStringExtra("uid");
        Toast.makeText(this, uid, Toast.LENGTH_SHORT).show();
        helper = new DatabaseHelper(this);
        db= helper.getReadableDatabase();
        aboutRecycler();

    }

    private void aboutRecycler() {
        HashMap<Class<?>, Pair<Integer, Integer>> map = new HashMap<>();
        map.put(String.class, Pair.create(R.layout.string_item, BR.string));
        map.put(AnchorSecondAvtivityVoiceData.class, Pair.create(R.layout.anchorsecondactivity_voice_item, BR.anchorSecondAvtivityVoiceData));
        map.put(AnchorSecondAvtivityAblumEntity.class, Pair.create(R.layout.anchorsecondactivity_album_items, BR.anchorSecondAvtivityAblumEntity));
        adapter = new CommentRecyclerAdapter<>(this, new ArrayList<>(), map);
        binding.recycler.setAdapter(adapter);
        application = ((BaseApplication) getApplicationContext());
        service = application.getService();
        aboutAppBarLayout();
        aboutService();
        aboutListener();
    }

    private void aboutListener() {
        binding.fold.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                  if(isClose){
                      close();
                      isClose=false;
                  }else {
                      open();
                      isClose=true;
                  }
            }
        });
        binding.activityAnchorAblumSecondCare.setOnClickListener(this);
        binding.activityAnchorAblumSecondCare2.setOnClickListener(this);
        binding.activityAnchorSecondShare.setOnClickListener(this);
        binding.activityAnchorSecondBack.setOnClickListener(this);
    }

    private void open() {
        binding.shortTitle.setVisibility(View.GONE);
        binding.longTitle.setVisibility(View.VISIBLE);
        ViewCompat.animate(binding.logo)
                .translationY(-300)
                .rotation(-720)
                .alpha(0)
                .setDuration(2000)
                .start();
        ViewCompat.animate(binding.nickName)
                .translationY(-100)
                .setDuration(2000)
                .start();
        ViewCompat.animate(binding.longTitle)
                .translationY(-100)
                .alpha(1)
                .setDuration(2000)
                .start();
        ViewCompat.animate(binding.shortTitle)
                .alpha(0)
                .setDuration(2000)
                .start();

    }

    private void close() {
        ViewCompat.animate(binding.logo)
                .translationY(10)
                .rotation(720)
                .alpha(1)
                .setDuration(2000)
                .start();
        ViewCompat.animate(binding.nickName)
                .translationY(10)
                .setDuration(2000)
                .start();
        ViewCompat.animate(binding.longTitle)
                .translationY(100)
                .alpha(0)
                .setDuration(2000)
                .start();
        ViewCompat.animate(binding.shortTitle)
                .alpha(1)
                .setDuration(2000)
                .start();
       binding.shortTitle.setVisibility(View.VISIBLE);
       binding.longTitle.setVisibility(View.GONE);
    }
    private void aboutAppBarLayout() {
        service.getAnchorSecondAvtivityEntity(uid).enqueue(new Callback<AnchorSecondAvtivityEntity>() {

            @Override
            public void onResponse(Call<AnchorSecondAvtivityEntity> call, Response<AnchorSecondAvtivityEntity> response) {
              binding.setAnchorSecondAvtivityEntity(response.body());
                mobileLargeLogo = response.body().getMobileLargeLogo();
                name = response.body().getNickname();
            }

            @Override
            public void onFailure(Call<AnchorSecondAvtivityEntity> call, Throwable t) {
                Toast.makeText(AnchorSecondActivity.this, "AnchorSecondActivity" + t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void aboutService() {
         service.getAnchorSecondAvtivityAblumEntity(uid).enqueue(new Callback<AnchorSecondAvtivityAblumEntity>() {
             @Override
             public void onResponse(Call<AnchorSecondAvtivityAblumEntity> call, Response<AnchorSecondAvtivityAblumEntity> response) {
                 adapter.add("发布的专辑(" + response.body().getList().size() + ")");
                 adapter.add(response.body());
                 if(response.body().getList().size()!=0){
                     aboutVoice();
                 }
             }

             @Override
             public void onFailure(Call<AnchorSecondAvtivityAblumEntity> call, Throwable t) {
                 Toast.makeText(AnchorSecondActivity.this, "AnchorSecondActivity" + t.getMessage(), Toast.LENGTH_SHORT).show();
             }
         });

    }

    private void aboutVoice() {
        service.getAnchorSecondAvtivityVoiceEntity(1, uid).enqueue(new Callback<AnchorSecondAvtivityVoiceEntity>() {
            @Override
            public void onResponse(Call<AnchorSecondAvtivityVoiceEntity> call, Response<AnchorSecondAvtivityVoiceEntity> response) {
                adapter.add("发布的声音(" + response.body().getList().size() + ")");
                adapter.addAll(response.body().getList());
            }

            @Override
            public void onFailure(Call<AnchorSecondAvtivityVoiceEntity> call, Throwable t) {
                Toast.makeText(AnchorSecondActivity.this, "AnchorSecondActivity" + t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_anchor_second_back:
                finish();
                break;
            case R.id.activity_anchor_second_share:
                application.showShare("我喜爱的主播","http://www.ximalaya.com/explore/",name,mobileLargeLogo);
            break;
            case R.id.activity_anchor_ablum_second_care:
                if(isClick){
                    isClick = false;
                    aboutDelete();
                    binding.activityAnchorAblumSecondCare.setVisibility(View.VISIBLE);
                    binding.activityAnchorAblumSecondCare2.setVisibility(View.GONE);
                }else{
                    isClick = true;
                    aboutInsert();
                    binding.activityAnchorAblumSecondCare.setVisibility(View.GONE);
                    binding.activityAnchorAblumSecondCare2.setVisibility(View.VISIBLE);
                }
            break;
        }
    }

    private void aboutDelete() {

    }

    private void aboutInsert() {
        ContentValues values = new ContentValues();
        values.put("name",name);
        values.put("img",mobileLargeLogo);
        values.put("uid",uid);
        db.insert(DatabaseHelper.TABLE_NAME1,null,values);
        Log.d("1","插入成功"+name +mobileLargeLogo+uid);
        db.close();
    }
}
